<?
include('../common/autoload.php');
use \PFT\Db;
include_once '/var/www/html/new/d/module/common/Db.class.php';
$conf =include '/var/www/html/new/d/module/common/db.conf.php';
Db::Conf($conf['remote_1']);
$Db = Db::Connect();

define('SECRET_KEY', 'RFGrfgY5CjVP8LcY');
switch($_REQUEST['action']){
    /*
    *更改活动状态
    *@param $marketing_status   0正常 2关闭 3删除
    *@param $marketing_id
    */
    case 'changeStatus':
        if(!is_numeric($_POST['marketing_status']) || !is_numeric($_POST['marketing_id']))
            exit('{"code":242,"msg":"参数错误"}');
        right_check('pft_marketing',$_POST['marketing_id'], $Db);
        $upd_stastus = "update pft_marketing set status=? where id=? and fid=? limit 1";
        // echo $upd_stastus;
        // exit;
        $stmt = $Db->prepare($upd_stastus);
        $stmt->execute(array($_POST['marketing_status'],$_POST['marketing_id'],$_SESSION['sid']));
        // exit('{"code":200,"msg":"状态已修改"}');
        echo '200';
        break;
    //根据分享页面的ID查询哪些用到这个页面的活动的名称
    case 'activity_name':
        if(!is_numeric($_POST['m_id']) )
            exit('{"code":242,"msg":"参数错误"}');
        right_check('pft_marketing_sp',$_POST['m_id'], $Db);
        $sel_active_name = "select activity_name from pft_marketing m left join pft_marketing_sp s
                                    on m.share_page=s.id
                                    where s.id=? and m.fid=? and s.fid=?";
        $stmt = $Db->prepare($sel_active_name);
        $stmt->execute(array($_POST['m_id'],$_SESSION['sid'],$_SESSION['sid']));
        $row = $stmt->fetchAll(PDO::FETCH_ASSOC);
        echo json_encode($row);
        break;
    //删除分享页面 sp表没有状态开关 只能把整行都删了
    case 'sp_delete':
        if(!is_numeric($_POST['id']))
            exit('{"code":242,"msg":"参数错误"}');
//        print_r(array($_POST['id'],$_SESSION['sid']));
        right_check('pft_marketing_sp',$_POST['id'], $Db);
        $del = "DELETE FROM pft_marketing_sp where id=? and fid=? limit 1";
        $stmt = $Db->prepare($del);
        $stmt->execute(array($_POST['id'],$_SESSION['sid']));
        if ($_POST['flag']==1) {
            //分享活动
            $del2 = "UPDATE pft_marketing SET status=3 WHERE share_page=? AND activity_type=3 LIMIT 1";
            $stmt = $Db->prepare($del2);
            $stmt->execute(array($_POST['id']));
        }
        echo '200';
        break;
    case 'Share_Page_List':
        //获取分享页面的列表
        $auth = md5(SECRET_KEY . 'Share_Page_List');
        $sign = md5($_SESSION['sid'].'Share_Page_List'.SECRET_KEY);
        $act  = 'Share_Page_List';
        $fid  = $_SESSION['sid'];
        $url  = "http://121.41.120.34/pft/marketing/marketing.php?action=$act&auth=$auth&sign=$sign&fid=$fid";
        $json = file_get_contents($url);
        if (!empty($json)) {
            echo '{"code":"200", "collection":'.$json.'}';
        } else {
            echo '{"code":"100"}';
        }
        break;
    case 'op_chk':
        break;
    default :
        break;
}

function right_check($table_name,$id, PDO $db){
    $sql = "select count(*) from $table_name where id=? and fid=?";
    $stmt = $db->prepare($sql);
    $stmt->execute(array($id,$_SESSION['sid']));
    $row = $stmt->fetchAll(PDO::FETCH_ASSOC);
//     print_r($row);
    // exit;
    return $row[0]['count(*)']=='1'?true:exit('{"code":241,"msg":"归属帐号错误"}');
}